package com.press.controllers.admin;

import com.alibaba.fastjson.JSONObject;
import com.press.bean.CommentBean;
import com.press.bean.UserBean;
import com.press.services.CommentService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YIJIE on 2016/10/14.
 */
@Controller
@RequestMapping("/CommentController")
public class CommentController {
    @Resource(name="commentService")
    private CommentService commentService;
    private static Logger log = Logger.getLogger(PanelController.class);

    public CommentService getCommentService() {
        return commentService;
    }

    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    @RequestMapping("/getComment")
    public void getComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        List<CommentBean> allCommentList =  commentService.getComment();
        List<CommentBean> publishedList = commentService.getPublished();
        List<CommentBean> checkList = commentService.getCheck();
        List<CommentBean> commentBinList = commentService.getBinComment();


        String commentCount = commentService.getcommentCount();
        String publishedCount = commentService.publishedCount();
        String checkCount = commentService.checkCount();
        String commentBinCount = commentService.commentBinCount();
        json.put("allCommentList",allCommentList);
        json.put("publishedList",publishedList);
        json.put("checkList",checkList);
        json.put("commentBinList",commentBinList);
        json.put("commentCount",commentCount);
        json.put("publishedCount",publishedCount);
        json.put("checkCount",checkCount);
        json.put("commentBinCount",commentBinCount);
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/putStick")
    public void putStick(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        boolean result = commentService.putStick(comment_id);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/cancelStick")
    public void cancelStick(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        boolean result = commentService.cancelStick(comment_id);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/passComment")
    public void passComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        boolean result = commentService.passComment(comment_id);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/nopassComment")
    public void nopassComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        boolean result = commentService.nopassComment(comment_id);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/deleteComment")
    public void deleteComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        boolean result = commentService.deleteComment(comment_id);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/giveTrash")
    public void giveTrash(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        boolean result = commentService.giveTrash(comment_id);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }

    @RequestMapping("/reply")
    public void reply(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String comment_id = request.getParameter("comment_id");
        CommentBean commentBean = commentService.getCommentById(comment_id);
        request.setAttribute("text",commentBean.getComment_text());
        request.setAttribute("nickname",commentBean.getaUser().getUser_nickname());
        request.setAttribute("commentId",commentBean.getComment_id());
        request.setAttribute("contentId",commentBean.getContent_id());
        request.getRequestDispatcher("/WEB-INF/admin/comment/replyComment.jsp").forward(request,response);

    }

    @RequestMapping("/addReply")
    public void addReply(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        JSONObject json = new JSONObject();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        Map map  = new HashMap<>();

        map.put("content_id",Integer.parseInt(request.getParameter("content_id")));
        map.put("user_id",request.getParameter("user_id"));
        map.put("comment_parentId",Integer.parseInt(request.getParameter("comment_id")));
        map.put("comment_text",request.getParameter("comment_text"));

        boolean result = commentService.addReply(map);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());


    }

}
